﻿using System;
using System.Windows.Data;
using System.Globalization;
using DXTools.Core.CrmSdk;

namespace DXTools.UI.Modules.MetadataBrowser.Converters
{


    public class ObjectConverter : IValueConverter
    {
        public object Convert(object value,
                           Type targetType,
                           object parameter,
                           CultureInfo culture)
        {
            object returnValue = value;

            if (value != null)
            {
                if (value.GetType() == typeof(EntityMetadata))
                {
                    string field = parameter as String;
                    EntityMetadata data = (EntityMetadata)value;

                }

                if (value.GetType() == typeof(Label))
                {
                    returnValue = ((Label)value).UserLocalizedLabel != null ? ((Label)value).UserLocalizedLabel.Label : String.Empty;
                }

                else if (value.GetType() == typeof(AttributeRequiredLevelManagedProperty))
                {
                    returnValue = ((AttributeRequiredLevelManagedProperty)value).Value.ToString() != "" ? ((AttributeRequiredLevelManagedProperty)value).Value.ToString() : String.Empty;
                }

                else if (value.GetType() == typeof(BooleanManagedProperty))
                {
                    returnValue = ((BooleanManagedProperty)value).Value.ToString() != "" ? ((BooleanManagedProperty)value).Value : false;
                }



                else if (value.GetType() == typeof(AssociatedMenuConfiguration))
                {
                    AssociatedMenuConfiguration obj = (AssociatedMenuConfiguration)value;
                    string config = "";

                    if (obj.Behavior != null)
                    {
                        config += "Behaviour: " + ((obj.Behavior != null) ? obj.Behavior.Value.ToString() : String.Empty);
                        config += ", Group: " + ((obj.Group != null) ? obj.Group.Value.ToString() : String.Empty);
                        config += ", Label: " + ((obj.Label.UserLocalizedLabel != null) ? obj.Label.UserLocalizedLabel.Label.ToString() : String.Empty);
                        config += ", Order: " + ((obj.Order != null) ? obj.Order.Value.ToString() : String.Empty);
                    }
                    returnValue = config;
                }

                else if (value.GetType() == typeof(CascadeConfiguration))
                {
                    CascadeConfiguration obj = (CascadeConfiguration)value;
                    string config = "";

                    config += "Assign: " + ((obj.Assign != null) ? obj.Assign.Value.ToString() : String.Empty);
                    config += ", Delete: " + ((obj.Delete != null) ? obj.Delete.Value.ToString() : String.Empty);
                    config += ", Share: " + ((obj.Share != null) ? obj.Share.Value.ToString() : String.Empty);
                    config += ", Unshare: " + ((obj.Unshare != null) ? obj.Unshare.Value.ToString() : String.Empty);
                    config += ",Reparent: " + ((obj.Reparent != null) ? obj.Reparent.Value.ToString() : String.Empty);
                    config += ", Merge: " + ((obj.Merge != null) ? obj.Merge.Value.ToString() : String.Empty);

                    returnValue = config;
                }
            }



            return returnValue;

        }

        public object ConvertBack(object value,
                                  Type targetType,
                                  object parameter,
                                  CultureInfo culture)
        {
            throw new NotImplementedException();

        }
    }
}